// pages/index/index.js
const App = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    buttonList: [{
      name: '最新通知',
      img: 'https://dyme-education.oss-cn-hangzhou.aliyuncs.com/static/notify.png',
      index: 1,
    }, {
      name: '音乐之旅',
      img: 'https://dyme-education.oss-cn-hangzhou.aliyuncs.com/static/journey.png',
      index: 2,
    }, {
      name: '考级报名',
      img: 'https://dyme-education.oss-cn-hangzhou.aliyuncs.com/static/sign_up.png',
      index: 3,
    }, {
      name: 'VIP课',
      img: 'https://dyme-education.oss-cn-hangzhou.aliyuncs.com/static/vip_course.png',
      index: 4,
    }, ],
    currentItem: 1,
    musicTypeParams: {
      pageSize: 10,
      pageNum: 1,
      soundCourseType: ""
    },
    curriculumParams: {
      pageSize: 10,
      pageNum: 1,
    },
    journeyList: [],
    musicTypes: [], //音乐之旅类型
    musicTypeCurrent: '', //音乐之旅列表
    curriculumList: [], //vip课程列表
    moreData: false,
    currentCourse: {}, //近期课程
    videoData: {
      // 音乐之旅视频
      status: false,
      url: "",
      title: ""
    },
    noticeList: [],
    qrcodeType: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},
  onShareAppMessage() {},
  async onShow() {
    if (wx.getStorageSync('environment')) {
      wx.switchTab({
        url: "/pages/teacher/teacher"
      });
      return
    }
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
    await this.getCurrentCourse()
    this.listGetdata(this.data.currentItem)
  },

  async getNoticeList() {
    // 消息列表查询
    let data = await App.$axios.request({
      url: "/user/pageNotice",
      method: 'GET',
      loading: true,
    })
    if (data.rows) {
      // data.rows.forEach(e => {
      //   e.noticeContent = e.noticeContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto" ')
      // })
      this.setData({
        noticeList: data.rows
      })
    }
  },

  async getMusicType() {
    // 音乐之旅-类别查询
    let data = await App.$axios.request({
      url: "/parent/musicTour/type",
      method: 'GET',
      loading: true,
    })
    if (data) {
      this.setData({
        musicTypes: [{
          label: '全部',
          type: ''
        }, ...data]
      })
    }
  },
  async getMusicTourList() {
    // 音乐之旅-分页查询
    let data = await App.$axios.request({
      url: "/parent/musicTour/page",
      method: 'GET',
      loading: true,
      data: {
        ...this.data.musicTypeParams
      }
    })
    if (data && data.rows) {
      this.setData({
        musicTypeParams: {
          ...this.data.musicTypeParams,
          pageNum: data.pageNum + 1
        },
        journeyList: [...this.data.journeyList, ...data.rows],
        moreData: data.total > data.pageNum * 10
      })
    }
  },
  async getCurriculum() {
    //  vip课程
    let data = await App.$axios.request({
      url: "/parent/curriculum/index/vipPage",
      method: 'GET',
      loading: true,
      data: {
        ...this.data.curriculumParams
      }
    })
    if (data && data.rows) {
      this.setData({
        curriculumParams: {
          ...this.data.curriculumParams,
          pageNum: data.pageNum + 1
        },
        curriculumList: [...this.data.curriculumList, ...data.rows],
        moreData: data.total > data.pageNum * 10
      })
    }
  },
  listGetdata: async function (index) {
    // 切换tab接口请求
    switch (index) {
      case 1:
        await this.getNoticeList()
        break;
      case 2:
        await this.getMusicType()
        await this.getMusicTourList()
        // console.log(App.$getDict())
        break;
      case 3:
        wx.navigateTo({
          url: '/pages/examination/examination',
        })
        break;
      case 4:
        this.getQrcode()
        this.getCurriculum()
        break;
      default:
        break;
    }
  },
  changeTab: function (item) {
    // 切换tab数据清除
    let itemIndex = item.currentTarget.dataset.iteminfo.index
    if (this.data.currentItem == itemIndex) return
    if (itemIndex != 3) {
      this.setData({
        currentItem: itemIndex,
        curriculumList: [],
        journeyList: [],
        musicTypeParams: {
          pageSize: 10,
          pageNum: 1,
          soundCourseType: ""
        },
        curriculumParams: {
          pageSize: 10,
          pageNum: 1,
        },
      })
    }
    this.listGetdata(itemIndex)
  },
  changeMusicType: function (item) {
    // 修改音乐之旅类型
    let current = item.currentTarget.dataset.type.type
    if (this.data.musicTypeCurrent == current) return
    this.setData({
      musicTypeCurrent: current,
      journeyList: [],
      musicTypeParams: {
        pageSize: 10,
        pageNum: 1,
        soundCourseType: current
      },
    })
    this.getMusicTourList()
  },
  getMoreData: function () {
    // 加载更多按钮
    let current = this.data.currentItem
    console.log(current)
    switch (current) {
      case 2:
        this.getMusicTourList()
        break;
      case 4:
        this.getCurriculum()
        break;
      default:
        break;
    }
  },
  jumpIntroduce: function (item) {
    // 跳转vip课程详情
    if (!item.currentTarget.dataset.item) return
    let params = item.currentTarget.dataset.item
    wx.navigateTo({
      url: `/pages/introduce/introduce?id=${params.id}`,
    })
  },
  getCurrentCourse: async function () {
    // 获取近期课程
    let data = await App.$axios.request({
      url: '/parent/curriculum/lately',
      method: 'GET',
      loading: true,
    })
    if (data) {
      this.setData({
        currentCourse: data
      })
    }
  },
  clickVideo(e) {
    let item = e.currentTarget.dataset.item
    this.setData({
      videoData: {
        status: true,
        url: item.musicUrl,
        title: item.musicName
      }
    })
  },
  videoClose() {
    this.setData({
      videoData: {
        status: false,
        url: "",
        title: ""
      }
    })
  },
  noticeClick(data) {
    let item = data.currentTarget.dataset.item
    console.log(item)
    // return
    wx.navigateTo({
      url: `/pages/notice/notice?id=${item.id}`,
    })
  },
  async getQrcode() {
    // 联系老师
    let data = await App.$axios.request({
      url: '/user/config/parent_contact_url',
      method: 'GET',
      loading: true,
    })
    if (data) {
      this.setData({
        qrcodeType: data
      })
    }
  }
})